﻿

//using var db = new SqlContext();
//var emp = new Employee() { Name = "lyf", Age = 26, Address = "JinShuiroad" };
//db.Employees.Add(emp);
//db.Works.Add(new Work() { Content="programmer", WorkArea="郑州", Employee = emp});
//await db.SaveChangesAsync();
Console.WriteLine("请输入用户名称：");
var userName = Console.ReadLine();
using var db = new SqlContext();
using var trans = db.Database.BeginTransaction();
var house = db.Houses.FromSqlInterpolated($" select top 1 * from T_House with(UPDLOCK) where id=1   ").FirstOrDefault();
Console.WriteLine("开始抢房....");
if (string.IsNullOrEmpty(house?.Owner))
{
    house.Owner = userName;
    await db.SaveChangesAsync();
    Thread.Sleep(15000);
    Console.WriteLine($"恭喜{userName}，抢房成功!");
}
else
{
    Console.WriteLine($"该房产已被{house.Owner} 抢走");
}


await trans.CommitAsync();
Console.ReadKey();